package com.oobi.showlive.cloudsql.data.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DBConnection {
    //static reference to itself
    private static DBConnection instance = new DBConnection();
    private Connection connection = null;
    public static final String URL = "jdbc:mysql://showlive.co17idupmyi0.us-east-1.rds.amazonaws.com:3306/showlive?user=showlivedb";
    public static final String USER = "showlivedb";
    public static final String PASSWORD = "showlivedb123";
    public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; 
 
    //private constructor
    private DBConnection() {
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 
    private Connection createConnection() {
        
        try {
        	if(connection == null) {
        		connection = DriverManager.getConnection(URL, USER, PASSWORD);
        	}
        } catch (SQLException e) {
            System.out.println("ERROR: Unable to Connect to Database.");
            e.printStackTrace();
        }
        return connection;
    }   
    
    private void close() {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
            }
        }
    }
 
    public static Connection getConnection() {
        return instance.createConnection();
    }
    
    public static void closeConnection() {
    	instance.close();
    }
    
}